package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.constant.ProductAttrTypeConstant;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimall.product.dao.AttrDao;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrAttrGroupVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {
    @Autowired
    AttrDao attrDao;
    @Autowired
    AttrService attrService;
    @Autowired
    AttrGroupService attrGroupService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public List<AttrEntity> listAttrToGroup(Long attrGroupId) {
        List<AttrAttrgroupRelationEntity> list = this.list(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId));
        List<Long> attrIds = list.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
        return !attrIds.isEmpty() ?attrDao.selectBatchIds(attrIds):null;
    }
    @Transactional
    @Override
    public PageUtils queryNoAttrRelation(Map<String, Object> params, Long attrGroupId) {
        //当前分组只能关联自己分类下的属性
        //当前分组只能关联别的分组 没有关联的
        //1.找出当前分类下的所有分组
        //2.获取这些分组关联的属性
        //3.从当前分类的所有属性中移除这些属性
        AttrGroupEntity groupEntity= attrGroupService.getById(attrGroupId);
        Long catelogId = groupEntity.getCatelogId();
        List<AttrGroupEntity> attrGroupEntities = attrGroupService.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<Long> attrGroupIds = attrGroupEntities.stream().map(AttrGroupEntity::getAttrGroupId).collect(Collectors.toList());
        List<AttrAttrgroupRelationEntity> selectedAttr = this.list(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", attrGroupIds));
        List<Long> selectedList = selectedAttr.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>().eq("catelog_id", catelogId).eq("attr_type", ProductAttrTypeConstant.ProductAttrType.ATTR_BASE.getCode());
        if (!selectedList.isEmpty()){
            wrapper.notIn("attr_id", selectedList);
        }
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.and((w)->w.eq("attr_id", key).or().like("attr_name", key));
        }

        IPage<AttrEntity> page = attrService.page(
                new Query<AttrEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public void removeAttrAttrGroupRelation(List<AttrAttrGroupVo> vos) {
        List<AttrAttrgroupRelationEntity> collect = vos.stream().map((vo) -> {
            AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
            entity.setAttrId(vo.getAttrId());
            entity.setAttrGroupId(vo.getAttrGroupId());
            return entity;
        }).collect(Collectors.toList());
        baseMapper.deleteBatchRelation(collect);
    }

    @Override
    public void saveBatchA(List<AttrAttrGroupVo> vos) {
        List<AttrAttrgroupRelationEntity> collect = vos.stream().map((vo) -> {
            AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
            entity.setAttrId(vo.getAttrId());
            entity.setAttrGroupId(vo.getAttrGroupId());
            return entity;
            }
        ).collect(Collectors.toList());
        this.saveBatch(collect);
    }

}